package org.dromara.event.controller;

import java.util.Date;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.mapper.SysUserMapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.event.domain.vo.EventAuditVo;
import org.dromara.event.domain.bo.EventAuditBo;
import org.dromara.event.service.IEventAuditService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 活动审核
 *
 * @author Lion Li
 * @date 2025-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/audit")
public class EventAuditController extends BaseController {

    private final IEventAuditService eventAuditService;

    private final SysUserMapper sysUserMapper;

    /**
     * 查询活动审核列表
     */
    @SaCheckPermission("event:audit:list")
    @GetMapping("/list")
    public TableDataInfo<EventAuditVo> list(EventAuditBo bo, PageQuery pageQuery) {
        TableDataInfo<EventAuditVo> tableDataInfo = eventAuditService.queryPageList(bo, pageQuery);
        //活动审核人名字放进去
        tableDataInfo.getRows().forEach(item -> {
            item.setUserName(sysUserMapper.selectVoById(item.getUserId()).getNickName());
        });
        return tableDataInfo;
    }

    /**
     * 导出活动审核列表
     */
    @SaCheckPermission("event:audit:export")
    @Log(title = "活动审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EventAuditBo bo, HttpServletResponse response) {
        List<EventAuditVo> list = eventAuditService.queryList(bo);
        ExcelUtil.exportExcel(list, "活动审核", EventAuditVo.class, response);
    }

    /**
     * 获取活动审核详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("event:audit:query")
    @GetMapping("/{id}")
    public R<EventAuditVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(eventAuditService.queryById(id));
    }

    /**
     * 新增活动审核
     */
    @SaCheckPermission("event:audit:add")
    @Log(title = "活动审核", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EventAuditBo bo) {
        return toAjax(eventAuditService.insertByBo(bo));
    }

    /**
     * 修改活动审核
     */
    @SaCheckPermission("event:audit:edit")
    @Log(title = "活动审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EventAuditBo bo) {
        return toAjax(eventAuditService.updateByBo(bo));
    }

    /**
     * 删除活动审核
     *
     * @param ids 主键串
     */
    @SaCheckPermission("event:audit:remove")
    @Log(title = "活动审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(eventAuditService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 审核活动人
     *
     * @param id 主键
     */
    @SaCheckPermission("event:audit:edit")
    @Log(title = "活动审核", businessType = BusinessType.UPDATE)
    @GetMapping("/audit/{id}")
    public R<Void> audit(@NotNull(message = "主键不能为空")
                         @PathVariable Long id,@RequestParam String auditStatus) {
        return toAjax(eventAuditService.audit(id,auditStatus));
    }


    /**
     * 修改活动审核状态成功
     */
    @SaCheckPermission("event:audit:edit")
    @Log(title = "活动审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editType")
    public R<Void> editType(@Validated(EditGroup.class) @RequestBody EventAuditBo bo) {
        bo.setApplyStatus("1");
        bo.setAuditUserId(LoginHelper.getUserId());
        bo.setAuditTime(new Date());
        return toAjax(eventAuditService.updateTpye(bo));
    }

    /**
     * 修改活动审核状态失败
     */
    @SaCheckPermission("event:audit:edit")
    @Log(title = "活动审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editTypeFail")
    public R<Void> editTypeFail(@Validated(EditGroup.class) @RequestBody EventAuditBo bo) {
        bo.setApplyStatus("2");
        bo.setAuditUserId(LoginHelper.getUserId());
        bo.setAuditTime(new Date());
        return toAjax(eventAuditService.updateTpye(bo));
    }

    /**
     * 报名接口
     */
    @SaCheckPermission("event:audit:edit")
    @Log(title = "活动审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/apply/{eventId}")
    public R<Void> apply(@PathVariable Long eventId) {
        EventAuditBo bo = new EventAuditBo();
        bo.setEventId(eventId);
        bo.setUserId(LoginHelper.getUserId());
        bo.setApplyStatus("0");
        return toAjax(eventAuditService.insertByBo(bo));
    }


}
